<template>

    <el-row  type="flex" class="row-bg" justify="center">
        <el-col :span="6">
            <h1>欢迎进入XXX管理系统</h1>
            <el-image :src="require('@/assets/logo.png')"></el-image>
        </el-col>
        <el-col :span="1">
            <el-divider direction="vertical"></el-divider>
        </el-col>
        <el-col :span="6">
            <el-form :model="loginForm" :rules="rules" ref="loginForm" label-width="100px" class="demo-ruleForm">
                <el-form-item label="用户名" prop="username">
                    <el-input v-model="loginForm.username"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input v-model="loginForm.password" type="password"></el-input>
                </el-form-item>
                <el-form-item label="验证码" prop="code">
                    <el-input v-model="loginForm.code" style="width: 120px ; float: left"></el-input>
                    <el-image :src="captchaImg" class="captchaImg" @click="getcaptchaImg"></el-image>
                </el-form-item>
                <div style="text-align:center">
                <el-button type="primary" @click="submitForm('loginForm')" >登录</el-button>
                    <el-button type="primary" @click="resetForm('loginForm')">重置</el-button>
                </div>
            </el-form>
        </el-col>
    </el-row>


</template>

<script>
    import qs from  'qs'
    export default {
        data() {
            return {
                loginForm: {
                    username: '',
                    password: '',
                    code: '',
                    token: '',
                },
                rules: {
                    username: [
                        { required: true, message: '请输入用户名', trigger: 'blur' }
                    ],
                    password: [
                        { required: true, message: '请输入密码', trigger: 'blur' }
                    ],
                    code: [
                        { required: true, message: '请输入验证码', trigger: 'blur' },
                        { min: 5, max: 5, message: '长度在 5个字符', trigger: 'blur' }
                    ],
                },
                captchaImg: null,
            };
        },
        methods: {
            submitForm(loginForm) {
                this.$refs[loginForm].validate((valid) => {
                    if (valid) {
                        this.$axios.post('/login?'+qs.stringify(this.loginForm)).then(res =>{

                            const jwt=res.headers['authorization']
                            this.$store.commit('SET_TOKEN',jwt)
                            this.$router.push("/index")

                        })
                    }
                    else {
                        this.$message.error('登录失败');
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            },
            getcaptchaImg(){
                this.$axios.get('/captcha').then(res =>{
                    console.log(res)
                    this.loginForm.token=res.data.data.token,
                    this.captchaImg=res.data.data.captchaImg,
                    this.loginForm.code=''
                })
            }
        },
        created() {
            this.getcaptchaImg()
        }
    }
</script>

<style scoped>

    .el-row{
        background-color: #F2F6FC;
        height: 100vh;
        align-items: center;
    }
    .el-divider{
        height: 200px;
    }
    .el-image{
        float: left;
        margin-left: 8px;
        border-radius: 4px;
    }
</style>
